package com.njwb.day15.zqzb.util;

import java.sql.*;

public class DbUtil {
    //开连接
    private static final String DRIVER_CLASS_NAME = "com.mysql.cj.jdbc.Driver";


    private static final String URL = "jdbc:mysql://localhost:3306/mytest";

    private static final String username = "root";

    private static final String password = "root" ;


    public static Connection getConnection(){
        Connection connection = null ;
        try {
            Class.forName(DRIVER_CLASS_NAME);
            connection  = DriverManager.getConnection(URL,username,password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection ;
    }

    public static void closeConnection(ResultSet rs, PreparedStatement pstmt,Connection connection){
        if(rs!=null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(pstmt!=null) {
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(connection!=null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }




    //关资源的方法
}
